package models

import (
	"errors"
	"time"

	"gorm.io/gorm"
)

type XinmanUser struct {
	Uuid       string    `gorm:"type:char(36);primaryKey;unique" json:"uuid"`                             //type:string       comment:                    version:2024-11-08 09:22
	Sort       int64     `gorm:"type:bigint(20) not null" json:"sort"`                                    //type:*int         comment:排序                version:2024-11-08 09:22
	Username   string    `gorm:"type:varchar(190) not null;unique" json:"username"`                       //type:string       comment:用户名              version:2024-11-08 09:22
	Email      string    `gorm:"type:varchar(190) not null;unique" json:"email"`                          //type:string       comment:邮箱                version:2024-11-08 09:22
	Phone      string    `gorm:"type:varchar(20)" json:"phone"`                                           //type:string       comment:手机号              version:2024-11-08 09:22
	Password   string    `gorm:"type:char(64) not null" json:"password"`                                  //type:string       comment:密码sha256哈希值    version:2024-11-08 09:22
	CreateTime time.Time `gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP" json:"createTime"`     //type:*time.Time   comment:用户注册时间        version:2024-11-08 09:22
	UpdateTime time.Time `gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'" json:"updateTime"` //type:*time.Time   comment:用户信息更新时间    version:2024-11-08 09:22
}

// TableName 表名:xinman_user，用户表信息。
// 说明:
func (XinmanUser) TableName() string {
	return "xinman_user"
}

func (user *XinmanUser) BeforeSave(tx *gorm.DB) (err error) {
	// 检查用户名是否为空
	if user.Username == "" {
		return errors.New("username cannot be empty")
	}
	// 检查密码是否为空
	if user.Password == "" {
		return errors.New("password cannot be empty")
	}
	if user.Email == "" {
		return errors.New("email cannot be empty")
	}
	return
}
